package com.bxd.baodan.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.fragment.MsgPlanerFragment;
import com.bxd.baodan.fragment.MsgQuestionFragment;
import com.bxd.baodan.list.MessageListFragment;
import com.bxd.baodan.util.MsgUtil;
import com.bxd.baodan.widget.PagerSlidingTabStrip;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息中心
 */
public class MsgActivity extends BaseActivity implements View.OnClickListener {

    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private int fromType;
    private ViewPager pager;
    private TextView all_read;
    private String[] strings = {"  动态  ", "  咨询  "};
    private MyFragmentPagerAdapter mAdapter;
    private PagerSlidingTabStrip tabs;
    String[] sysMsgType = {Constant.MSG_NEW_ACTIVITIES + "", Constant.MSG_NEW_READ + "", Constant.MSG_NEW_VIDEO + "",
            Constant.MSG_NEW_BAODAN + ""};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg);
        fromType=getIntent().getIntExtra("from",0);
        hideBar();
        initView();
    }

    private void initView() {
        all_read = (TextView) findViewById(R.id.all_read);
        all_read.setOnClickListener(this);
        pager = (ViewPager) findViewById(R.id.pager);
        tabs = (PagerSlidingTabStrip) findViewById(R.id.strip);
        fragmentList.add(MessageListFragment.newInstance(sysMsgType));
        fragmentList.add(new MsgPlanerFragment());
        //fragmentList.add(new MsgQuestionFragment());//去除问答
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(mAdapter);
        tabs.setViewPager(pager);
        setTabsValue();
        tabs.setMsgToast(1, true);

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        MobclickAgent.onEvent(App.getContext(), "v6_1_3_BW_xx_dt");
                        break;
                    case 1:
                        MobclickAgent.onEvent(App.getContext(), "v6_1_3_BW_xx_zx");

                        break;
                    case 2:
                        MobclickAgent.onEvent(App.getContext(), "v6_1_3_BW_xx_wd");
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        switch (fromType){
            case 1:
                pager.setCurrentItem(1);
                break;
            default:
                pager.setCurrentItem(0);
                break;
        }
    }

    private void setTabsValue() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        // 设置Tab底部选中的指示器Indicator的高度
        tabs.setIndicatorHeight(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, dm));
        // 设置Tab底部选中的指示器 Indicator的颜色
        tabs.setIndicatorColorResource(R.color.green_lab);
        //设置指示器Indicatorin是否跟文本一样宽，默认false
        tabs.setIndicatorinFollowerTv(false);
        //设置小红点提示，item从0开始计算，true为显示，false为隐藏
        //          tabs.setMsgToast(1, true);
        //设置红点滑动到当前页面自动消失,默认为true
        tabs.setMsgToastPager(true);
        //设置Tab标题文字的颜色
        tabs.setTextColor(getResources().getColor(R.color.black));
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, dm));
        // 设置选中的Tab文字的颜色
        tabs.setSelectedTextColorResource(R.color.green_lab);
        //设置Tab底部分割线的高度
        tabs.setUnderlineHeight(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.5f, dm));
        //设置Tab底部分割线的颜色
        tabs.setUnderlineColorResource(R.color.transparent);
        // 设置点击某个Tab时的背景色,设置为0时取消背景色tabs.setTabBackground(0);
        //        tabs.setTabBackground(R.drawable.bg_tab);
        tabs.setTabBackground(0);
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(false);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.all_read:
                MsgUtil.setImReaded();
                MsgUtil.allPushRead();
                ((MessageListFragment) fragmentList.get(0)).allRead();
                ((MsgPlanerFragment) fragmentList.get(1)).notifyChanged();
                //((MsgQuestionFragment) fragmentList.get(2)).getDbData();
                tabs.setMsgToast(0, false);
                tabs.setMsgToast(1, false);
                //tabs.setMsgToast(2, false);
                break;
        }
    }


    public void back(View v) {
        finish();
    }


    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            return fragmentList.get(arg0);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return strings[position];
        }
    }

    public void setPlanerRed(boolean show) {
        tabs.setMsgToast(1, show);
    }

    public void setQesRed(boolean show) {
        tabs.setMsgToast(2, show);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (MsgUtil.getSysPushMsg()) {
            tabs.setMsgToast(0, true);
        } else {
            tabs.setMsgToast(0, false);
        }

        if (MsgUtil.getImUnread()) {
            tabs.setMsgToast(1, true);
        } else {
            tabs.setMsgToast(1, false);
        }

//        if (MsgUtil.getAnsPushMsg()) {
//            tabs.setMsgToast(2, true);
//        } else {
//            tabs.setMsgToast(2, false);
//        }

    }
}
